home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Toolbox / Visual Basic Toolbox (P.I.E.)(1996).ISO / lang_ext / vbawk / utils.bas < prev    next >
Encoding:
BASIC Source File  |  1994-08-12  |  5.3 KB  |  172 lines

  1. Dim StaticTokenStr As String    'used by StrTok
  2. Dim StaticNdx As Integer        'used by StrTok
  3.  
  4. Function FileExists (Filename As String) As Integer
  5.     On Error GoTo notthere
  6.     Temp% = FileLen(Filename)
  7.     FileExists = True
  8.     Exit Function
  9. notthere:
  10.     FileExists = False
  11.     Exit Function
  12. End Function
  13.  
  14. Function gsub (TheString As String, SearchFor As String, Subst As String) As Integer
  15.     If SearchFor = "" Then
  16.         MsgBox "Error in gsub: 0-length search string."
  17.         Exit Function
  18.     End If
  19.     StartAt% = 1
  20.     SearchForLen% = Len(SearchFor)
  21.     SubstLen% = Len(Subst)
  22.     WhereFound% = InStr(StartAt%, TheString, SearchFor, COMPAREMODE)
  23.     While WhereFound%
  24.         Substitute TheString, WhereFound%, SearchForLen%, Subst
  25.         numfound% = numfound% + 1
  26.         StartAt% = WhereFound% + SubstLen%
  27.         WhereFound% = InStr(StartAt%, TheString, SearchFor, COMPAREMODE)
  28.     Wend
  29.     gsub = numfound%
  30. End Function
  31.  
  32. Function ReverseString (TheStr As String) As String
  33.     For i% = Len(TheStr) To 1 Step -1
  34.         retval$ = retval$ & Mid$(TheStr, i%, 1)
  35.     Next i%
  36.     ReverseString = retval$
  37. End Function
  38.  
  39. Sub SplitFilePath (ByVal ThePath$, TheDrive$, TheDirs$, TheFile$, TheExtension$)
  40.  
  41.     'Initialization
  42.     PathNdx% = 1
  43.     ThePathLen% = Len(ThePath$)
  44.  
  45.     'Is there a drive specifier?
  46.     TheDrive$ = ""
  47.     If InStr(ThePath$, ":") = 2 Then
  48.         TheDrive$ = Left$(ThePath$, 1)
  49.         PathNdx% = 3
  50.     End If
  51.     If PathNdx% > ThePathLen% Then
  52.         'there was nothing but a drive specifier, so quit
  53.         Exit Sub
  54.     End If
  55.  
  56.     'Are there directories?
  57.     TheDirs$ = ""
  58.     LastSlashNdx% = 0
  59.     SlashNdx% = InStr(PathNdx%, ThePath$, "\")
  60.     While SlashNdx% > 0
  61.         LastSlashNdx% = SlashNdx%
  62.         SlashNdx% = InStr(SlashNdx% + 1, ThePath$, "\")
  63.     Wend
  64.     If LastSlashNdx% <> 0 Then
  65.         TheDirs$ = Mid$(ThePath$, PathNdx%, (LastSlashNdx% - PathNdx%))
  66.         PathNdx% = LastSlashNdx% + 1
  67.     End If
  68.     If PathNdx% > ThePathLen% Then
  69.         'there was nothing after the directories, so quit
  70.         Exit Sub
  71.     End If
  72.  
  73.     'Get the filename
  74.     TheFile$ = ""
  75.     TheExtension$ = ""
  76.     DotNdx% = InStr(LastSlashNdx% + 1, ThePath$, ".")
  77.     If DotNdx% > 0 Then
  78.         'filename has an extension, so extract it
  79.         TheFile$ = Mid$(ThePath$, PathNdx%, DotNdx% - PathNdx%)
  80.         TheExtension$ = Right$(ThePath$, ThePathLen% - DotNdx%)
  81.     Else
  82.         TheFile$ = Right$(ThePath$, ThePathLen% - LastSlashNdx%)
  83.     End If
  84. End Sub
  85.  
  86. 'STRTOK:
  87. 'This routine is closely analagous to the standard 'C' string
  88. 'function with the same name.
  89. '
  90. 'StrTok parses a string, which is assumed to consist of
  91. 'tokens separated by delimiters.  Each Time you call StrTok,
  92. 'it returns the next token in the string.
  93. '
  94. 'The first time you call it, TokenStr should be the string
  95. 'to parse, and Delimiters is the string containing characters
  96. 'that delimit the tokens.  For instance, if the tokens are
  97. 'separated by either commas or blanks, Delimiters should be
  98. 'the following:  " ,"
  99. '
  100. 'On succeeding calls to StrTok, TokenStr should be a zero-
  101. 'length string ("").  StrTok will remember what TokenStr was.
  102. 'However, TokenStr can be anything you want it to be; thus,
  103. 'you can look for different delimiters on succeeding calls.
  104. Function StrTok (ByVal TokenStr As String, Delimiters As String) As String
  105.     On Error GoTo finished
  106.     StrTok = ""
  107.     If TokenStr <> "" Then
  108.         'on first call, copy token string to static var
  109.         StaticTokenStr = TokenStr
  110.         StaticNdx = 1
  111.     End If
  112.     'while leftmost chars in StaticTokenStr are delimiters,
  113.     'throw them out.
  114.     While True
  115.         TheChar$ = Mid$(StaticTokenStr, StaticNdx, 1)
  116.         If TheChar$ = "" Then Exit Function
  117.         If InStr(Delimiters, TheChar$) > 0 Then
  118.             StaticNdx = StaticNdx + 1
  119.         Else
  120.             GoTo FoundToken
  121.         End If
  122.     Wend
  123.  
  124. FoundToken:
  125.     'while succeeding chars in StaticTokenStr are not
  126.     'delimiters, pass them by
  127.     j% = StaticNdx
  128.     While True
  129.         TheChar$ = Mid$(StaticTokenStr, j%, 1)
  130.         If TheChar$ = "" Then GoTo EndOfToken
  131.         If InStr(Delimiters, TheChar$) > 0 Then GoTo EndOfToken
  132.         j% = j% + 1
  133.     Wend
  134.  
  135. EndOfToken:
  136.     'return the token and update the static token string.
  137.     StrTok = Mid$(StaticTokenStr, StaticNdx, j% - StaticNdx)
  138.     StaticNdx = j%
  139.     Exit Function
  140.  
  141. finished:
  142.     MsgBox "Error: " & Error$ & " in StrTok"
  143.     Exit Function
  144. End Function
  145.  
  146. Sub Substitute (TheText$, StartLoc%, TheLen%, Subst$)
  147.     Dim LocalStart As Integer
  148.     Dim LocalLen As Integer
  149.     Dim TheTextLen As Integer
  150.  
  151.     'sanity checking on start location
  152.     TheTextLen = Len(TheText$)  'for optimization, since Len is referred to often
  153.     If StartLoc% > (TheTextLen + 1) Then
  154.         LocalStart = (TheTextLen + 1)
  155.     Else
  156.         LocalStart = StartLoc%
  157.     End If
  158.  
  159.     'sanity checking on length
  160.     If TheLen% > (TheTextLen - StartLoc% + 1) Then
  161.         LocalLen = (TheTextLen - StartLoc% + 1)
  162.     Else
  163.         LocalLen = TheLen%
  164.     End If
  165.  
  166.     'perform the substitution
  167.     LeftPart$ = Left$(TheText$, LocalStart - 1)
  168.     RightPart$ = Right$(TheText$, TheTextLen - LocalStart - LocalLen + 1)
  169.     TheText$ = LeftPart$ & Subst$ & RightPart$
  170. End Sub
  171.  
  172.